package com.example.cushion.base;

import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import com.example.cushion.R;
import com.example.cushion.ui.loading.ILoading;
import com.example.cushion.ui.loading.LoadingView;

import org.itzheng.and.baseutils.ui.UIUtils;

/**
 * Title:标题带下划线的Activity，如语言选择等界面那种效果<br>
 * Description: <br>
 * <p>
 * email:ItZheng@ZoHo.com
 * Created by itzheng on 2017-10-11.
 */
public class BaseCommonActivity extends BaseActivity {
    private View layoutToolbar;
    private View layoutNavigation;
    private ViewGroup layoutContentView;
    private TextView tvToolbarTitle;
    private android.support.v7.widget.Toolbar toolbar;
    private View.OnClickListener mNavigationOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTranslucentStatus(true);
        super.setContentView(R.layout.frame_activity_base_common);
        setStatusBarDarkMode(true);
        initTitleBar();
    }

    /**
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 初始化标题栏
     */
    private void initTitleBar() {
        layoutToolbar = findViewById(R.id.layoutToolbar);
        layoutNavigation = findViewById(R.id.layoutNavigation);
        layoutContentView = findViewById(R.id.layoutContentView);
        tvToolbarTitle = findViewById(R.id.tvToolbarTitle);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        layoutNavigation.setOnClickListener(mNavigationOnClickListener);

    }


    public void setHideTitleBar(boolean on) {
        if (layoutToolbar != null) {
            layoutToolbar.setVisibility(on ? View.GONE : View.VISIBLE);
        }
    }

    /**
     * 设置返回键点击事件,如果没有设置则使用系统默认（返回）
     *
     * @param onClickListener
     */
    public void setNavigationOnClickListener(View.OnClickListener onClickListener) {
        mNavigationOnClickListener = onClickListener;
        if (layoutNavigation != null) {
            layoutNavigation.setOnClickListener(mNavigationOnClickListener);
        }
    }

    /**
     * 设置标题栏颜色，可设置透明，状态栏的颜色并不一定就是标题栏的颜色，状态栏是根据底色自动显示的
     *
     * @param color getResources().getColor(R.color.colorAccent)
     */
    public void setTitleBarColor(@ColorInt int color) {
        if (layoutToolbar != null) {
            layoutToolbar.setBackgroundColor(color);
        }
    }

    /**
     * 设置标题文本颜色
     *
     * @param textColor Color.RED
     */
    @Override
    public void setTitleColor(int textColor) {
        tvToolbarTitle.setTextColor(textColor);
    }

    @Override
    public void setTitle(CharSequence title) {
        tvToolbarTitle.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getText(titleId));
    }

    @Override
    public void setContentView(final View view) {
        if (layoutContentView != null) {
            layoutContentView.removeAllViews();
            if (view != null) {
                removeParent(view);
                layoutContentView.addView(view);
            }

        }
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(UIUtils.inflate(this, layoutResID));
    }

    private void removeParent(View view) {
        if (view == null) {
            return;
        }
        if (view.getParent() == null) {
            return;
        }
        ((ViewGroup) view.getParent()).removeView(view);
    }
}
